package jp.ac.u_tokyo.dch.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
/**
* @version 1.1
*/
public class DchLogger extends Logger{

	/**
	 *シリアル番号
	 */
	private static final long serialVersionUID = 783789011768005131L;

	/**
	 * 生成者
	 * @param name 名前
	 */
	protected DchLogger(String name) {
		super(name);
	}

	/**
	 * ロガーを返す
	 * @param pclass クラス
	 * @return ロガー
	 */
	public static DchLogger getLogger(Class<?> pclass){
		 LoggerFactory factory = new DchLoggerFactory();
	        return (DchLogger) LogManager.getLogger(pclass.getName(), factory);
    }


	/**
	 * エラーログを書く
	 * @param e 例外
	 */
	public  void error(Exception e) {
		StringBuffer sb = null;
		try {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);

			sb = sw.getBuffer();

			sw.flush();
			sw.close();
			pw.flush();
			pw.close();
		} catch (IOException ioe) { }

		if (sb != null) {
			this.error(sb.toString());
		} else {
			this.error("");
		}
   }

}
